package com.bootcamp.s2_0922.ds.Exercise._0930_Ex;

/**
 * @program: myClassCode
 * @description: given two strings, compare them and find the additional char.
 * @author: Keyu Li
 * @create: 2021-09-30 19:32
 **/

public class Ex1_FindChar {
    public static void main(String[] args) {

        String s1 = "ajfhkjadsnfksd";
        String s2 = "ajfhkjbadsnfksd";
        System.out.println(findLetter(s1, s2));
    }

    public static char findLetter(String s1, String s2){
        // 定义一个 a-z 数组存放每个字母
        int[] letterCount = new int[26];
        // 将s1按照字母下标存档到该数组
        for (int i = 0; i < s1.length(); i++) {
            char temp = s1.charAt(i);
            letterCount[temp-'a']++;
        }
        // 遍历 s2
        for (int i = 0; i < s2.length(); i++) {
            char temp = s2.charAt(i);
            if (letterCount[temp-'a']==0)
                return temp;
            letterCount[temp-'a']--;
        }
        return 0;
    }
}
